//
//  BooleanButtonGroupView.m
//  YiJuZhangShou2.0
//
//  Created by URANUS on 16/3/24.
//  Copyright © 2016年 Mr.X. All rights reserved.
//

#import "BooleanButtonGroupView.h"
#import "BooleanButtonLabelModel.h"
#import "BooleanButtonLabelView.h"
#import "Masonry.h"

@interface BooleanButtonGroupView()<BooleanButtonLabelViewDelegate>
@property (nonatomic, strong) NSMutableArray *booleanButtonArray;
@property (nonatomic, strong) NSMutableArray *booleanSelectedButtonArray;
@end

@implementation BooleanButtonGroupView

#pragma mark Delegate  BooleanButtonLabelViewDelegate
- (void)selectBooleanButtonWithState:(Boolean)state andButton:(UIButton *)selectedButton{
   
    BooleanButtonLabelView *parentView = (BooleanButtonLabelView *)[selectedButton superview];
    if (state == NO) {
        
        if ([self.booleanSelectedButtonArray containsObject:parentView]) {
            
            if(_isMulti){
            
                if (self.booleanSelectedButtonArray.count>1) {
                    [self.booleanSelectedButtonArray removeObject:parentView];
                    [self.selectedIndexArray removeObject:[NSNumber numberWithUnsignedInteger:[self.booleanButtonArray indexOfObject:parentView]]];
                }else{
                    [parentView setIsSelect:YES];
                }
                
            }else{
                [parentView setIsSelect:YES];
            }
           
        }
    }else{
      
        if (_isMulti) {
            [self.booleanSelectedButtonArray addObject:parentView];
           [self.selectedIndexArray addObject:[NSNumber numberWithUnsignedInteger:[self.booleanButtonArray indexOfObject:parentView]]];
        }else{
            
            if (self.booleanSelectedButtonArray.count>0) {
               BooleanButtonLabelView *selectView = self.booleanSelectedButtonArray[0];
               selectView.isSelect = NO;
                [self.booleanSelectedButtonArray removeObject:selectView];
            }
           [self.booleanSelectedButtonArray addObject:parentView];
            _selectedIndex = [self.booleanButtonArray indexOfObject:parentView];
        }
        
    }
   
}

#pragma mark - Private
- (void)loadBooleanButtonGroupView{
    UIView *lastView;
    
    if (_isVertical) {
        
        for (int i = 0; i<self.booleanButtonArray.count; i++) {
            
            UIView *view = self.booleanButtonArray[i];
            
            if (lastView == nil) {
                
                [view mas_remakeConstraints:^(MASConstraintMaker *make) {
                    make.left.top.right.equalTo(self);
                }];
            }else{
               
                if (i == self.booleanButtonArray.count - 1) {
                    
                    [view mas_remakeConstraints:^(MASConstraintMaker *make) {
                        make.left.right.equalTo(self);
                        make.top.equalTo(view.mas_bottom);
                        make.height.equalTo(view);
                        make.bottom.equalTo(self);
                    }];
                    
                }else{
                    
                    [view mas_remakeConstraints:^(MASConstraintMaker *make) {
                        make.left.right.equalTo(self);
                        make.top.equalTo(view.mas_bottom);
                        make.height.equalTo(view);
                    }];
                }
                
            }
            
            lastView = self.booleanButtonArray[i];
        }
       
    }else{
    
        for (int i = 0; i<self.booleanButtonArray.count; i++) {
            
            UIView *view = self.booleanButtonArray[i];
            
            if (lastView == nil) {
                
                [view mas_remakeConstraints:^(MASConstraintMaker *make) {
                    make.top.bottom.equalTo(self);
                    if (_isStartRight == NO) {
                        make.left.equalTo(self);
                    }
                }];
            }else{
                
                if (i == self.booleanButtonArray.count - 1) {
                    
                    [view mas_remakeConstraints:^(MASConstraintMaker *make) {
                        make.left.equalTo(lastView.mas_right).offset(10);
                        make.top.bottom.equalTo(lastView);
                        if (_isStartRight == YES) {
                            make.right.equalTo(self);
                        }
                    }];
                    
                }else{
                    [view mas_remakeConstraints:^(MASConstraintMaker *make) {
                        make.left.equalTo(lastView.mas_right).offset(10);
                        make.top.bottom.equalTo(lastView);
                    }];
                }
                
            }
            
            lastView = self.booleanButtonArray[i];
        }
    }
    
}

#pragma mark - Setter
-(void)setBooleanButtonModelGroup:(NSMutableArray *)booleanButtonModelGroup{
    _booleanButtonModelGroup = booleanButtonModelGroup;
    _booleanButtonArray = [NSMutableArray array];
    _booleanSelectedButtonArray = [NSMutableArray array];
    _selectedIndexArray = [NSMutableArray array];
    
    for (UIView *view in self.subviews) {
        if (view.tag == 1) {
            [view removeFromSuperview];
        }
    }
    
    for (int i=0; i<booleanButtonModelGroup.count; i++) {
        BooleanButtonLabelView *view = [BooleanButtonLabelView new];
        view.delegate = self;
        view.tag = 1;
        [self addSubview:view];
        BooleanButtonLabelModel *model = booleanButtonModelGroup[i];
        view.model = model;
        [self.booleanButtonArray addObject:view];
    }
    [self loadBooleanButtonGroupView];
}

-(void)setIsVertical:(Boolean)isVertical{
    _isVertical = isVertical;
    [self loadBooleanButtonGroupView];
}

-(void)setIsStartRight:(Boolean)isStartRight{
    _isStartRight = isStartRight;
    [self loadBooleanButtonGroupView];
}

@end
